<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    SQL从删库到跑路(二) |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-SQL从删库到跑路-二" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  SQL从删库到跑路(二)
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/09/09/SQL%E4%BB%8E%E5%88%A0%E5%BA%93%E5%88%B0%E8%B7%91%E8%B7%AF-%E4%BA%8C/" class="article-date">
  <time datetime="2020-09-09T04:45:02.000Z" itemprop="datePublished">2020-09-09</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/SQL/">SQL</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="一、DML：增删改表中数据"><a href="#一、DML：增删改表中数据" class="headerlink" title="一、DML：增删改表中数据"></a>一、DML：增删改表中数据</h1><h3 id="1-添加数据"><a href="#1-添加数据" class="headerlink" title="1. 添加数据"></a>1. 添加数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 常规写法</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu(<span class="keyword">sid</span>,sname,sage) <span class="keyword">values</span>(<span class="number">172922</span>,<span class="string">'朱同心'</span>,<span class="number">22</span>);</span><br><span class="line"><span class="comment">-- 简便写法</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu <span class="keyword">values</span>(<span class="number">17200</span>,<span class="string">'刘升升'</span>,<span class="number">21</span>,<span class="literal">null</span>,<span class="literal">null</span>);</span><br></pre></td></tr></table></figure>

<h3 id="2-删除数据"><a href="#2-删除数据" class="headerlink" title="2. 删除数据"></a>2. 删除数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">delete</span> <span class="keyword">from</span> 表名 whrere 条件;</span><br><span class="line"><span class="comment">-- 注意：如果不加条件则删除表中所有记录</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 删除表，然后再创建一个一模一样的空表(效率高)</span></span><br><span class="line"> <span class="keyword">truncate</span> <span class="keyword">table</span> stu;</span><br></pre></td></tr></table></figure>

<h3 id="3-修改数据"><a href="#3-修改数据" class="headerlink" title="3. 修改数据"></a>3. 修改数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">update</span> 表名 <span class="keyword">set</span> 列名<span class="number">1</span>=值<span class="number">1</span>，列名<span class="number">2</span>=值<span class="number">2</span> <span class="keyword">where</span> 条件</span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 将朱同心的名字改为猪大头，年龄改为24</span></span><br><span class="line"> <span class="keyword">update</span> stu <span class="keyword">set</span> sname=<span class="string">'猪大头'</span>,sage=<span class="number">24</span> <span class="keyword">where</span> sname=<span class="string">'朱同心'</span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 注意：如果不加条件则会将表中所有记录全部修改</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h1 id="二、DQL：查询语句"><a href="#二、DQL：查询语句" class="headerlink" title="二、DQL：查询语句"></a>二、DQL：查询语句</h1><h2 id="1-排序查询"><a href="#1-排序查询" class="headerlink" title="1. 排序查询"></a>1. 排序查询</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">order</span> <span class="keyword">by</span> math <span class="comment">-- 排序(默认升序)</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">order</span> <span class="keyword">by</span> math <span class="keyword">desc</span> <span class="comment">-- 降序</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">order</span> <span class="keyword">by</span> math <span class="keyword">asc</span> <span class="comment">-- 升序</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--  按照数学成绩排名，如果数学成绩一样，则按照英语成绩排名</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">order</span> <span class="keyword">by</span> math <span class="keyword">asc</span>,english <span class="keyword">asc</span>;</span><br><span class="line"><span class="comment">-- 按照数学成绩排名，如果数学成绩一样，则按照英语成绩倒序排名</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">order</span> <span class="keyword">by</span> math ,english <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure>

<p><strong>注意：如果有多个排序条件，则当前面的条件值一样时，才会判断第二条件</strong></p>
<h2 id="2-聚合函数"><a href="#2-聚合函数" class="headerlink" title="2. 聚合函数"></a>2. 聚合函数</h2><ul>
<li>将一列数据作为一个整体，进行纵向的计算</li>
</ul>
<table>
<thead>
<tr>
<th>聚合函数</th>
<th>用途</th>
</tr>
</thead>
<tbody><tr>
<td>count</td>
<td>计算个数</td>
</tr>
<tr>
<td>max</td>
<td>计算最大值</td>
</tr>
<tr>
<td>min</td>
<td>计算最小值</td>
</tr>
<tr>
<td>sum</td>
<td>计算和</td>
</tr>
<tr>
<td>avg</td>
<td>计算平均值</td>
</tr>
</tbody></table>
<ul>
<li><strong>注意：聚合函数的计算，排除null值</strong></li>
<li>解决方案<blockquote>
<ol>
<li>选择不包含非空的列计算  </li>
<li>ifnull函数</li>
</ol>
</blockquote>
</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">count</span>(<span class="keyword">name</span>) <span class="keyword">from</span> stu;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">count</span>(<span class="keyword">ifnull</span>(english,<span class="number">0</span>)) <span class="keyword">from</span> stu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">max</span>(math) <span class="keyword">from</span> stu;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">min</span>(math) <span class="keyword">from</span> stu;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(math) <span class="keyword">from</span> stu;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">avg</span>(math) <span class="keyword">from</span> stu;</span><br></pre></td></tr></table></figure>


<h2 id="3-分组查询"><a href="#3-分组查询" class="headerlink" title="3. 分组查询"></a>3. 分组查询</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"> group by 分组字段;</span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 按照性别分组，分别查询男、女同学的平均分</span></span><br><span class="line"> <span class="keyword">SELECT</span> sex,<span class="keyword">AVG</span>(math)	<span class="keyword">from</span> stu  <span class="keyword">GROUP</span> <span class="keyword">BY</span> sex;</span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 按照性别分组，分别查询男、女同学的平均分</span></span><br><span class="line"><span class="keyword">SELECT</span> sex,<span class="keyword">AVG</span>(math)	<span class="keyword">from</span> stu  <span class="keyword">GROUP</span> <span class="keyword">BY</span> sex;</span><br><span class="line"><span class="comment">-- 按照性别分组，分别查询男、女同学的平均分，人数</span></span><br><span class="line"><span class="keyword">SELECT</span> sex,<span class="keyword">AVG</span>(math),<span class="keyword">count</span>(<span class="keyword">id</span>) <span class="keyword">from</span> stu <span class="keyword">GROUP</span> <span class="keyword">BY</span> sex;</span><br><span class="line"><span class="comment">-- 按照性别分组，分别查询男、女同学的平均分 ，人数。 要求低于60分的不参与分组</span></span><br><span class="line"><span class="keyword">select</span> sex,<span class="keyword">avg</span>(math),<span class="keyword">count</span>(<span class="keyword">id</span>) <span class="keyword">from</span> stu <span class="keyword">where</span> math&gt;<span class="number">60</span> <span class="keyword">group</span> <span class="keyword">by</span> sex;</span><br><span class="line"><span class="comment">-- 按照性别分组，分别查询男、女同学的平均分 ，人数。要求低于60分的不参与分组,分组后人数要大于2人</span></span><br><span class="line"><span class="keyword">select</span> sex,<span class="keyword">avg</span>(math),<span class="keyword">count</span>(<span class="keyword">id</span>) <span class="keyword">from</span> stu <span class="keyword">where</span> math&gt;<span class="number">60</span> <span class="keyword">group</span> <span class="keyword">by</span> sex <span class="keyword">having</span> <span class="keyword">count</span>(<span class="keyword">id</span>)&gt;<span class="number">2</span>;</span><br></pre></td></tr></table></figure>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-07_23-34-34.jpg" alt=""></p>
<p><strong>where和having的区别？</strong></p>
<ul>
<li>where在分组之前进行限定，如果不满足条件，则不参与分组。having在分组之后进行限定，如果不满足结果，则不会被查询出来</li>
<li>where 后不可以跟聚合函数，having可以进行聚合函数的判断</li>
</ul>
<h2 id="4-分页查询"><a href="#4-分页查询" class="headerlink" title="4. 分页查询"></a>4. 分页查询</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 每页显示3条记录</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">limit</span> <span class="number">0</span>,<span class="number">3</span>;  </span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">limit</span> <span class="number">3</span>,<span class="number">3</span>; </span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">limit</span> <span class="number">6</span>,<span class="number">3</span>; </span><br><span class="line"><span class="comment">-- 公式：开始的索引=（当前的页码-1）*每页显示的条数</span></span><br></pre></td></tr></table></figure>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-07_23-55-11.jpg" alt=""></p>
<h2 id="5-基础查询"><a href="#5-基础查询" class="headerlink" title="5. 基础查询"></a>5. 基础查询</h2><p>语法</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>(字段列表) <span class="keyword">from</span>(表名列表) <span class="keyword">where</span>(条件列表) <span class="keyword">group</span> <span class="keyword">by</span>(分组字段) <span class="keyword">having</span>(分组后的条件) <span class="keyword">order</span> <span class="keyword">by</span>(排序) <span class="keyword">limit</span>(分页限定)</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>,address <span class="keyword">from</span> stu;</span><br><span class="line"><span class="comment">-- 去除重复的结果集</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> age <span class="keyword">from</span> stu;</span><br><span class="line"><span class="comment">-- 要去重必须保证结果集完全一样</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> <span class="keyword">name</span>,address <span class="keyword">from</span> stu</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 计算math和english分数之和,采用四则运算</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>,math,english,math+english <span class="keyword">as</span> sum_score <span class="keyword">from</span> stu;</span><br><span class="line"><span class="comment">-- 如果有null参与运算，计算结果都为null，可用math+ifnull(english,0)</span></span><br></pre></td></tr></table></figure>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_11-57-47.jpg" alt=""></p>
<h2 id="6-条件查询"><a href="#6-条件查询" class="headerlink" title="6. 条件查询"></a>6. 条件查询</h2><h4 id="①运算符"><a href="#①运算符" class="headerlink" title="①运算符"></a>①<strong>运算符</strong></h4><table>
<thead>
<tr>
<th>比较运算符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&gt;、&lt;、&lt;=、&gt;=、=、&lt;&gt;</td>
<td>&lt;&gt;在SQL中表示不等于，在mysql中也可以使用!=,没有==</td>
</tr>
<tr>
<td>between…and</td>
<td>在一个范围之内，如：between 100 and 200 ,相当于条件在100到200之间，包头又包尾</td>
</tr>
<tr>
<td>in(集合)</td>
<td>集合表示多个值，使用逗号分隔</td>
</tr>
<tr>
<td>like ‘张%’</td>
<td>模糊查询</td>
</tr>
<tr>
<td>is null</td>
<td>查询某一列为null的值，注意：不能写=null</td>
</tr>
</tbody></table>
<h4 id="②逻辑运算符"><a href="#②逻辑运算符" class="headerlink" title="②逻辑运算符"></a>②<strong>逻辑运算符</strong></h4><table>
<thead>
<tr>
<th>逻辑运算符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>and 或 &amp;&amp;</td>
<td>与，SQL中建议使用前者，后者并不通用</td>
</tr>
<tr>
<td>or 或 ||</td>
<td>或</td>
</tr>
<tr>
<td>not 或！</td>
<td>非</td>
</tr>
</tbody></table>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询英语成绩大于60且年龄大于22岁的学生</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">where</span> english&gt;<span class="number">60</span> <span class="keyword">and</span> age &gt;<span class="number">22</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄22岁，25岁，35岁的学生信息</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">where</span> age=<span class="number">21</span> <span class="keyword">or</span> age=<span class="number">24</span> <span class="keyword">or</span> age=<span class="number">25</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">where</span> age <span class="keyword">in</span>(<span class="number">21</span>,<span class="number">24</span>,<span class="number">25</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询英语成绩为null</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">where</span> english <span class="keyword">is</span> <span class="literal">null</span>;</span><br><span class="line"><span class="comment">-- 查询英语成绩不为null</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu <span class="keyword">where</span> english <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">null</span>;</span><br></pre></td></tr></table></figure>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_14-25-26.jpg" alt=""></p>
<h4 id="③模糊查询"><a href="#③模糊查询" class="headerlink" title="③模糊查询"></a>③模糊查询</h4><p>占位符</p>
<ul>
<li>_:单个任意字符</li>
<li>%:任意多个字符</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_15-02-15.jpg" alt=""></p>
<h1 id="三、约束"><a href="#三、约束" class="headerlink" title="三、约束"></a>三、约束</h1><h2 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h2><blockquote>
<p>对表中的数据进行限定，保证数据的正确性、有效性和完整性</p>
</blockquote>
<h2 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h2><h3 id="1-非空约束：not-null"><a href="#1-非空约束：not-null" class="headerlink" title="1. 非空约束：not null"></a>1. <strong>非空约束：not null</strong></h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建表添加非空约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> students(</span><br><span class="line">         <span class="keyword">id</span> <span class="built_in">int</span>,</span><br><span class="line">		<span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建表完成后添加非空约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">modify</span> <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>;</span><br></pre></td></tr></table></figure>

<h3 id="2-唯一约束：unique"><a href="#2-唯一约束：unique" class="headerlink" title="2. 唯一约束：unique"></a>2. <strong>唯一约束：unique</strong></h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--  创建表时添加唯一约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> students(</span><br><span class="line">            <span class="keyword">id</span> <span class="built_in">int</span>;</span><br><span class="line">						phone_num varchar(20) unique;</span><br><span class="line">);</span><br><span class="line"><span class="comment">--  注意mysql中唯一约束限定的列的值可以有多个null</span></span><br><span class="line"><span class="comment">--  删除唯一约束(常称为唯一索引)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">drop</span> <span class="keyword">index</span> phone_num;</span><br><span class="line"><span class="comment">--  创建表后，添加唯一约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">modify</span> phone_num <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">unique</span>;</span><br></pre></td></tr></table></figure>

<h3 id="3-主键约束：primary-key"><a href="#3-主键约束：primary-key" class="headerlink" title="3. 主键约束：primary key"></a>3. <strong>主键约束：primary key</strong></h3><ul>
<li>含义：非空且唯一（综合以上）</li>
<li>一张表只能有一个字段为主键</li>
<li>主键就是表中记录的唯一标识</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建表时添加主键约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> students(</span><br><span class="line">             <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span>,</span><br><span class="line">						 <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) </span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 删除主键</span></span><br><span class="line"><span class="comment">-- alter table students modify id int;(错误)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">drop</span> primary <span class="keyword">key</span>;</span><br><span class="line"><span class="comment">-- 创建完表后，添加主键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">modify</span> <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span>;</span><br></pre></td></tr></table></figure>

<p><strong>自动增长</strong>：如果某一列是数值类型的，使用auto_increment可以来完成值的自动增长</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 在创建表时，添加主键约束，并且完成主键自增长</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> students(</span><br><span class="line">       <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">       <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除自动增长</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">modify</span> <span class="keyword">id</span> <span class="built_in">int</span>;</span><br><span class="line"><span class="comment">-- 添加自动增长</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> students <span class="keyword">modify</span> <span class="keyword">id</span> <span class="built_in">int</span>; auto_increment</span><br></pre></td></tr></table></figure>

<h3 id="4-外键约束：foreign-key"><a href="#4-外键约束：foreign-key" class="headerlink" title="4. 外键约束：foreign key"></a>4. <strong>外键约束：foreign key</strong></h3><p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_22-21-59.jpg" alt=""></p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_22-41-26.jpg" alt=""></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--  创建部门表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> department(</span><br><span class="line">         <span class="keyword">id</span> <span class="built_in">int</span> PRIMARY <span class="keyword">key</span> AUTO_INCREMENT,</span><br><span class="line">				 dep_name <span class="built_in">varchar</span>(<span class="number">20</span>),</span><br><span class="line">				 dep_location <span class="built_in">VARCHAR</span>(<span class="number">20</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">--  创建员工表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> employee(</span><br><span class="line">        <span class="keyword">id</span> <span class="built_in">INT</span> PRIMARY <span class="keyword">KEY</span> AUTO_INCREMENT,</span><br><span class="line">				<span class="keyword">name</span> <span class="built_in">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">				age <span class="built_in">int</span>,</span><br><span class="line">				dep_id <span class="built_in">int</span>, <span class="comment">-- 外键对应主表的主键</span></span><br><span class="line">				<span class="keyword">CONSTRAINT</span> emp_dept_fk <span class="keyword">FOREIGN</span> <span class="keyword">KEY</span> (dep_id) <span class="keyword">REFERENCES</span> department(<span class="keyword">id</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">--  添加两个部门</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> department <span class="keyword">values</span>(<span class="literal">null</span>,<span class="string">'研发部'</span>,<span class="string">'广州'</span>),(<span class="literal">null</span>,<span class="string">'销售部'</span>,<span class="string">'深圳'</span>);</span><br><span class="line"><span class="comment">--  添加员工，dep_id表示员工所在的部门</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'张三'</span>,<span class="number">21</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'李四'</span>,<span class="number">20</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'王五'</span>,<span class="number">20</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'大王'</span>,<span class="number">22</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'小王'</span>,<span class="number">18</span>,<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> department;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">--  删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">drop</span> <span class="keyword">foreign</span> <span class="keyword">key</span> emp_dept_fk;</span><br><span class="line"></span><br><span class="line"><span class="comment">--  执行成功证明外键删除成功</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee(<span class="keyword">name</span> ,age,dep_id)<span class="keyword">values</span> (<span class="string">'小王'</span>,<span class="number">18</span>,<span class="number">5</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">--  添加外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">add</span> <span class="keyword">CONSTRAINT</span> emp_dept_fk <span class="keyword">FOREIGN</span> <span class="keyword">KEY</span> (dep_id) <span class="keyword">REFERENCES</span> department(<span class="keyword">id</span>);</span><br></pre></td></tr></table></figure>

<h3 id="外键约束-级联操作"><a href="#外键约束-级联操作" class="headerlink" title="外键约束_级联操作"></a><strong>外键约束_级联操作</strong></h3><ul>
<li><strong>需求</strong>：要将部门表的id进行修改操作。</li>
<li><strong>分析</strong>：不能直接进行修改，因为部门表的id值作为外键与员工表的dep_id关联着</li>
<li><strong>解决方案一</strong>：将员工表设置外键的值同部门表对应的值统一修改，然后再修改部门表的id</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-08_23-30-57.jpg" alt=""></p>
<p>然鹅，这样太麻烦了，有没有简单点的方法？</p>
<ul>
<li><p><strong>解决方案二</strong>：设置级联</p>
<p><strong>① 级联更新</strong>，当修改部门表的主键时，员工表中所有的外键会自动改为与部门表主键一致</p>
</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-09_12-34-52.jpg" alt=""></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 级联更新</span></span><br><span class="line"><span class="comment">-- 删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">drop</span> <span class="keyword">foreign</span> <span class="keyword">key</span> emp_dept_fk;</span><br><span class="line"><span class="comment">-- 添加外键时设置级联更新</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">add</span> <span class="keyword">CONSTRAINT</span> emp_dept_fk <span class="keyword">FOREIGN</span> <span class="keyword">KEY</span> (dep_id) <span class="keyword">REFERENCES</span> department(<span class="keyword">id</span>) <span class="keyword">on</span> <span class="keyword">update</span> <span class="keyword">cascade</span>;</span><br><span class="line"><span class="comment">-- 修改部门表的id</span></span><br><span class="line"><span class="keyword">update</span> department <span class="keyword">set</span> <span class="keyword">id</span>=<span class="number">1</span> <span class="keyword">where</span> dep_name=<span class="string">'研发部'</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> employee;</span><br></pre></td></tr></table></figure>

<p><strong>② 级联删除</strong>，当删除部门表的某一数据时，与其关联的员工表的数据也应自动删除</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-09-09_12-47-17.jpg" alt=""></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 级联删除</span></span><br><span class="line"><span class="comment">-- 删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">drop</span> <span class="keyword">foreign</span> <span class="keyword">key</span> emp_dept_fk;</span><br><span class="line"><span class="comment">-- 添加外键时设置级联更新，设置级联删除</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">add</span> <span class="keyword">CONSTRAINT</span> emp_dept_fk <span class="keyword">FOREIGN</span> <span class="keyword">KEY</span> (dep_id) <span class="keyword">REFERENCES</span> department(<span class="keyword">id</span>) <span class="keyword">on</span> <span class="keyword">update</span> <span class="keyword">cascade</span> <span class="keyword">on</span> <span class="keyword">delete</span> <span class="keyword">cascade</span>;</span><br><span class="line"><span class="comment">-- 删除部门表中的id为1的数据</span></span><br><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> department <span class="keyword">where</span> <span class="keyword">id</span>=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> employee;</span><br></pre></td></tr></table></figure>
      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/DML/" rel="tag">DML</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/DQL/" rel="tag">DQL</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SQL/" rel="tag">SQL</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%BA%A7%E8%81%94%E6%93%8D%E4%BD%9C/" rel="tag">级联操作</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/09/13/jdbc%E8%AF%A6%E8%A7%A3/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            jdbc详解
          
        </div>
      </a>
    
    
      <a href="/2020/09/06/SQL%E4%BB%8E%E5%88%A0%E5%BA%93%E5%88%B0%E8%B7%91%E8%B7%AF-%E4%B8%80/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">SQL从删库到跑路(一)</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>